﻿using System.Web.Query.Dynamic;

namespace Yonkly
{
    using System;
    using System.Web;
    using System.Text;

    public static class DataTimeExtension
    {
        public static string Ago(this DateTime targetUTC)
        {
            StringBuilder result = new StringBuilder();
            TimeSpan diff = (DateTime.Now.ToUniversalTime() - targetUTC);

            if (diff.Days > 0)
            {
                result.AppendFormat("{0} days", diff.Days);
            }

            if (diff.Hours > 0)
            {
                if (result.Length > 0)
                {
                    result.Append(", ");
                }

                result.AppendFormat("{0} hours", diff.Hours);
            }

            if (diff.Minutes > 0)
            {
                if (result.Length > 0)
                {
                    result.Append(", ");
                }

                result.AppendFormat("{0} minutes", diff.Minutes);
            }

            if (result.Length == 0)
            {
                result.Append("few moments ago");
            }
            else
            {
                result.Insert(0, "about ");
                result.AppendFormat(" ago");
            }

            return result.ToString();
        }
    }

    public static class StringExtension
    {
        public static string UrlEncode(this string target)
        {
            if (target.IndexOf(" ") > -1)
            {
                target = target.Replace(" ", "_");
            }

            if (target.IndexOf("#") > -1)
            {
                target = target.Replace("#", "sharp");
            }

            if (target.IndexOf("&") > -1)
            {
                target = target.Replace("&", "amp");
            }

            if (target.IndexOf(".") > -1)
            {
                target = target.Replace(".", "dot");
            }

            if (target.IndexOf("/") > -1)
            {
                target = target.Replace("/", "fws");
            }

            if (target.IndexOf("\\") > -1)
            {
                target = target.Replace("\\", "bks");
            }

            target = HttpUtility.UrlEncode(target);

            return target;
        }

        public static string UrlDecode(this string target)
        {
            target = HttpUtility.UrlDecode(target);

            if (target.IndexOf("_") > -1)
            {
                target = target.Replace("_", " ");
            }

            if (target.IndexOf("sharp") > -1)
            {
                target = target.Replace("sharp", "#");
            }

            if (target.IndexOf("amp") > -1)
            {
                target = target.Replace("amp", "&");
            }

            if (target.IndexOf("dot") > -1)
            {
                target = target.Replace("dot", ".");
            }

            if (target.IndexOf("fws") > -1)
            {
                target = target.Replace("fws", "/");
            }

            if (target.IndexOf("bks") > -1)
            {
                target = target.Replace("bks", "\\");
            }

            return target;
        }

        public static int ToMonth(this string target)
        {
            switch (target.ToLower())
            {
                case "jan":
                    return 1;
                case "feb":
                    return 2;
                case "mar":
                    return 3;
                case "apr":
                    return 4;
                case "may":
                    return 5;
                case "jun":
                    return 6;
                case "jul":
                    return 7;
                case "aug":
                    return 8;
                case "sep":
                    return 9;
                case "oct":
                    return 10;
                case "nov":
                    return 11;
                case "dec":
                    return 12;
                default:
                    throw new ParseException("Invalid month", 0);
            }
        }
        public static DateTime ToDateFromTwitterDate(this string target)
        {
            var month = target.Substring(4, 3).ToMonth();
            var day = Convert.ToInt32(target.Substring(8, 2));
            int hour = Convert.ToInt32(target.Substring(11, 2));
            int minute = Convert.ToInt32(target.Substring(14, 2));
            int second = Convert.ToInt32(target.Substring(17, 2));
            var year = Convert.ToInt32( target.Substring(26, 4));
            return new DateTime(year, month, day, hour, minute, second);
        }
    }

    public static class BoolExtensions
    {
        public static string ToCheckboxChecked(this bool value)
        {
            return value ? "checked='checked'" : "";
        }
    }


}